package com.olda.play.auth.security.exception;

import com.olda.play.enums.ResponseCode;
import com.olda.play.rpc.CommonResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class SecurityExceptionHandler {

    /**
     * 仅允许post请求
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(AuthenticationServiceException.class)
    public CommonResponseDto<Object> handleUsernameNotFoundException(UsernameNotFoundException e) {
        return CommonResponseDto.error(ResponseCode.ONLY_POST);
    }


    /**
     * 账户异常(禁用、锁定、过期)
     *
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({InternalAuthenticationServiceException.class})
    public CommonResponseDto handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        return CommonResponseDto.error(e.getMessage());
    }


}
